﻿Public Class FrmLogin

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Application.Exit()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If TextBox1.Text.Trim = String.Empty Then
            MsgBox("用户名不能为空!")
            Return
        End If
        Try
            SControl = New BServer.ServiceClient
            FServerIP = My.Computer.FileSystem.ReadAllText(Application.StartupPath & "\server.txt").Trim
            SControl = New BServer.ServiceClient(SControl.Endpoint.Name, String.Format("http://{0}/Service.svc?wsdl", FServerIP))
            UInfo = New BServer.FUserInfo
            Binfo = New BServer.FConfig
        Catch ex As Exception
            MsgBox(ex.Message)
            Return
        End Try
        Try
            Dim ErrString As String = String.Empty
            If SControl._User_Login(TextBox1.Text, TextBox2.Text, UInfo, ErrString) = False Then
                MsgBox(ErrString)
                Return
            End If
            If UInfo.FSession.Trim = String.Empty Then
                MsgBox("登录失败!服务器返回无法理解的信息")
                Return
            End If
            '--读取全局参数
            If SControl._Base_GetGlobalConfig(UInfo.FSession, Binfo, ErrString) = False Then
                MsgBox(String.Format("读取系统设置失败!{0}错误信息:{1}", vbCrLf, ErrString))
                Return
            End If
            '--检查版本
            Dim sver As String = SControl._BASE_CheckVer
            PVer = Replace(PVer, ".", "", , , CompareMethod.Text)
            If sver <> String.Empty Then
                If sver <> PVer Then
                    Dim mepath As String = Application.StartupPath
                    Try
                        My.Computer.FileSystem.DeleteDirectory(Application.StartupPath & "\update", FileIO.DeleteDirectoryOption.DeleteAllContents)
                    Catch ex As Exception

                    End Try

                    Try
                        System.IO.Directory.CreateDirectory(mepath & "\update")
                    Catch ex As Exception
                        MsgBox(ex.Message)
                        Return
                    End Try

                    '----把更新程序复制到更新文件夹下,并将路径和版本号加密后写入
                    System.IO.File.Copy(mepath & "\u.exe", mepath & "\update\u.exe", True)
                    System.IO.File.Copy(mepath & "\rar.exe", mepath & "\update\rar.exe", True)
                    System.IO.File.WriteAllText(mepath & "\update\p.ud", CControl.desencryp(mepath, "67771256"))
                    System.IO.File.WriteAllText(mepath & "\update\v.ud", CControl.desencryp("http://" & FServerIP & "/update.rar", "67784321"))

                    Process.Start(mepath & "\update\u.exe")
                    Application.Exit()
                End If
            End If
            '--记住用户名
            If CheckBox1.Checked Then
                My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\Ru.l", TextBox1.Text.Trim, False)
            Else
                My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\Ru.l", "", False)
            End If
            '--记住密码
            If CheckBox2.Checked Then
                My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\Rp.l", CControl.desencryp(TextBox2.Text.Trim, "qwerasdf"), False)
            Else
                My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\Rp.l", "", False)
            End If
            Hide()
            FrmMain.Show()
        Catch ex As Exception
            MsgBox(String.Format("登录失败!{0}错误信息:{1}", vbCrLf, ex.Message))
        End Try
    End Sub

    Private Sub FrmLogin_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Icon = My.Resources.Properties
        'Try
        '    FServerIP = My.Computer.FileSystem.ReadAllText(Application.StartupPath & "\server.txt").Trim
        '    SControl = New BServer.ServiceClient(SControl.Endpoint.Name, String.Format("http://{0}/Service.svc?wsdl", FServerIP))
        'Catch ex As Exception
        '    MsgBox(ex.Message)
        'End Try
        Label3.Text = "版本号:Ver " & PVer
        Try
            Dim fU As String = My.Computer.FileSystem.ReadAllText(Application.StartupPath & "\Ru.l")
            If fU.Trim <> String.Empty Then
                CheckBox1.Checked = True
                TextBox1.Text = fU.Trim
            End If
        Catch ex As Exception
            CheckBox1.Checked = False
        End Try
        Try
            Dim fP As String = My.Computer.FileSystem.ReadAllText(Application.StartupPath & "\Rp.l")
            If fP.Trim <> String.Empty Then
                TextBox2.Text = CControl.desdecryp(fP.Trim, "qwerasdf")
                CheckBox2.Checked = True
            End If
        Catch ex As Exception
            CheckBox2.Checked = False
        End Try

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim f1 As New FrmNetSetting
        f1.ShowDialog()
        f1 = Nothing
    End Sub
End Class
